package com.have.blog.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.domain.dto.ResourceCategoryDto;
import com.have.blog.admin.service.ResourceCategoryService;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.ResourceCategoryMapper;
import com.have.blog.model.mapper.VideoMapper;
import com.have.blog.model.models.ResourceCategory;
import com.have.blog.model.models.ResourceCategoryExample;
import com.have.blog.model.models.VideoExample;
import com.have.blog.picture.service.PictureFeignApi;
import com.have.blog.utils.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
public class ResourceCategoryServiceImpl implements ResourceCategoryService {
    @Autowired
    private ResourceCategoryMapper resourceCategoryMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private PictureFeignApi pictureFeignApi;

    @Override
    public Wrapper stickResourceCategory(ResourceCategoryDto resourceCategoryDto) {
        PageHelper.startPage(1, 1);
        ResourceCategoryExample example = new ResourceCategoryExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("sort desc");
        List<ResourceCategory> list = resourceCategoryMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return Wrapper.success();
        }
        ResourceCategory resourceSort = resourceCategoryMapper.selectByPrimaryKey(resourceCategoryDto.getUid());
        ResourceCategory maxSort = list.get(0);
        if (StringUtils.isEmpty(maxSort.getUid())) {
            return Wrapper.illegal();
        }
        if (maxSort.getUid().equals(resourceSort.getUid())) {
            return Wrapper.success(AdminMessageConstant.THIS_SORT_IS_TOP);
        }
        resourceSort.setSort(maxSort.getSort() + 1);
        resourceSort.setUpdateTime(new Date());
        int result = resourceCategoryMapper.updateByPrimaryKeySelective(resourceSort);
        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.OPERATION_SUCCESS);
        }
        return Wrapper.failed();
    }

    @Override
    public Wrapper deleteBatchResourceCategory(List<ResourceCategoryDto> resourceCategoryDto) {
        List<Integer> uids = new ArrayList<>();
        resourceCategoryDto.forEach(item -> {
            uids.add(item.getUid());
        });
        VideoExample videoExample = new VideoExample();
        videoExample.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andResourceCategoryUidIn(uids);
        long count = videoMapper.countByExample(videoExample);
        if (count > 0) {
            return Wrapper.failed(AdminMessageConstant.RESOURCE_UNDER_THIS_SORT);
        }

        ResourceCategory template = new ResourceCategory();
        template.setStatus(BlogStatus.DISABLE.getStatus());
        template.setUpdateTime(new Date());

        ResourceCategoryExample example = new ResourceCategoryExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andUidIn(uids);
        int result = resourceCategoryMapper.updateByExampleSelective(template, example);
        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.DELETE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.DELETE_FAIL);
    }

    @Override
    public Wrapper editResourceCategory(ResourceCategoryDto resourceCategoryDto) {
        ResourceCategory resourceSort = resourceCategoryMapper.selectByPrimaryKey(resourceCategoryDto.getUid());

        if (!resourceSort.getSortName().equals(resourceCategoryDto.getSortName())) {
            ResourceCategoryExample example = new ResourceCategoryExample();
            example.createCriteria()
                    .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                    .andSortNameEqualTo(resourceCategoryDto.getSortName());
            long count = resourceCategoryMapper.countByExample(example);
            if (count > 0) {
                return Wrapper.failed(AdminMessageConstant.ENTITY_EXIST);
            }
        }
        resourceSort.setSortName(resourceCategoryDto.getSortName());
        resourceSort.setContent(resourceCategoryDto.getContent());
        resourceSort.setCoverPictureUid(resourceCategoryDto.getCoverPictureUid());
        resourceSort.setSort(resourceCategoryDto.getSort());
        resourceSort.setUpdateTime(new Date());
        int result = resourceCategoryMapper.updateByPrimaryKeySelective(resourceSort);
        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.UPDATE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.UPDATE_FAIL);
    }

    @Override
    public Wrapper addResourceCategory(ResourceCategoryDto resourceCategoryDto) {
        ResourceCategoryExample example = new ResourceCategoryExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andSortNameEqualTo(resourceCategoryDto.getSortName());
        long count = resourceCategoryMapper.countByExample(example);
        if (count > 0) {
            return Wrapper.failed(AdminMessageConstant.ENTITY_EXIST);
        }

        ResourceCategory resourceSort = new ResourceCategory();
        resourceSort.setSortName(resourceCategoryDto.getSortName());
        resourceSort.setContent(resourceCategoryDto.getContent());
        resourceSort.setCoverPictureUid(resourceCategoryDto.getCoverPictureUid());
        resourceSort.setSort(resourceCategoryDto.getSort());
        resourceSort.setStatus(BlogStatus.ENABLE.getStatus());
        resourceSort.setUpdateTime(new Date());
        resourceSort.setCreateTime(new Date());
        int result = resourceCategoryMapper.insert(resourceSort);
        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.INSERT_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.INSERT_FAIL);
    }

    @Override
    public List<ResourceCategoryDto> getList(ResourceCategoryDto resourceCategoryDto) {
        PageHelper.startPage(resourceCategoryDto.getPageNum(), resourceCategoryDto.getPageSize());
        ResourceCategoryExample example = new ResourceCategoryExample();
        ResourceCategoryExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(resourceCategoryDto.getKeyword()) && !StringUtils.isEmpty(resourceCategoryDto.getKeyword().trim())) {
            criteria.andSortNameLike("%" + resourceCategoryDto.getKeyword() + "%");
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("sort desc");
        List<ResourceCategory> list = resourceCategoryMapper.selectByExample(example);
        List<ResourceCategoryDto> resultList = MyBeanUtils.copyListProperties(list, ResourceCategoryDto::new);


        List<Integer> picUids = new ArrayList<>();
        resultList.forEach(item -> {
            if (item.getCoverPictureUid() != null) {
                picUids.add(item.getCoverPictureUid());
            }
        });
        if (!CollectionUtils.isEmpty(picUids)) {
            Wrapper<Map<Integer, String>> picWrapper = pictureFeignApi.getPicturesUrl(picUids);
            if (picWrapper.isSuccess()) {
                Map<Integer, String> picMap = picWrapper.getData();
                for (ResourceCategoryDto categoryDto : resultList) {
                    categoryDto.setPhotoUrl(picMap.get(categoryDto.getCoverPictureUid()));
                }
            }
        }
        return resultList;
    }
}
